package com.hileto.inf.utils.dao;

import java.util.Arrays;
import java.util.List;

import com.hileto.inf.utils.ObjectHelp;

public class PageInfo<T>{
	private static final Integer MAX_PAGE_SIZE = 500;// 每页最大记录数限制
	private Integer currPageNo = 1;
	private Integer pageSize = 20;

	private List<T> result ;
	private Long totalCount;
	private Integer totalPages;
	
	public PageInfo(Integer currPageNo ,Integer pageSize) {
		this.setCurrPageNo(currPageNo);
		this.setPageSize(pageSize);
	}
	public PageInfo(Integer pageSize) {
		this(1, pageSize);
	}

	public Integer getCurrPageNo() {
		return currPageNo;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public List<T> getResult() {
		return result;
	}

	public void setResult(List<T> result) {
		this.result = result;
	}
	public Long getTotalCount() {
		return totalCount;
	}
	public T getTName(){
		return null;
	}

	public void setCurrPageNo(Integer currPageNo) {
		if (null != currPageNo){
			if (currPageNo < 1) {
				this.currPageNo = 1;
			}else{
				this.currPageNo = currPageNo;
			}
		}
	}
	public void setPageSize(Integer pageSize) {
		if(null != pageSize){
			if (pageSize > 1) {
				this.pageSize = pageSize;
			} else if(pageSize > MAX_PAGE_SIZE) {
				this.pageSize = MAX_PAGE_SIZE;
			}
		}
	}
	public Integer getTotalPages() {
		if(null == totalPages){
			totalPages = 0;
		}
		return totalPages;
	}
	
	public Long getOffset() {
		Long offset = 0L;
		offset = (this.currPageNo - 1) * this.pageSize + 0L;
		return offset;
	}
	
	@Override
	public String toString() {
		return "PageInfo [currPageNo=" + currPageNo + ", pageSize=" + pageSize
				+ ", totalCount=" + totalCount + ", totalPages=" + totalPages
				+ (ObjectHelp.isNotEmpty(result)?Arrays.toString(result.toArray()):"")
				+ "]";
	}
	
	public void setTotalCount(Long count) {
		if(null == count || count < 1){
			this.totalCount = 0L;
		}else{
			this.totalCount = count;
		}
		totalPages = totalCount%pageSize == 0 ? totalCount.intValue()/pageSize : totalCount.intValue()/pageSize+1 ;
	}
	
	public String proccessSQL(String baseSql) {
		baseSql += " limit " + this.getPageSize() + " offset " + this.getOffset();
		return baseSql;
	}
}
